package test.luin.pers.myapplication;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

public class PersonProvider extends ContentProvider {
    private static UriMatcher uriMatcher = new UriMatcher(-1);
    private static final int SUCCESS = 1;
    private PersonDBHelper dbHelper;

    static {
        uriMatcher.addURI("test.luin.pers.myapplication", "info", SUCCESS);
    }

    @Override
    public boolean onCreate() {
        dbHelper = new PersonDBHelper(getContext());
        return false;
    }

    //查询数据操作
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        //匹配查询的Uri路径
        int code = uriMatcher.match(uri);
        if(code==SUCCESS){
            SQLiteDatabase db = dbHelper.getReadableDatabase();
            return db.query("info",projection,selection,selectionArgs,null,null,sortOrder);
        }else{
            throw new IllegalArgumentException("路径不正确，我是不会给你提供数据的！");
        }
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues contentValues) {
        int code = uriMatcher.match(uri);
        if (code == SUCCESS) {
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            long row = db.insert("info", null, contentValues);
            if (row > 0) {
                Uri inserUri = ContentUris.withAppendedId(uri, row);
                getContext().getContentResolver().notifyChange(inserUri, null);
            }
            db.close();
            return uri;
        } else {
            throw new IllegalArgumentException("路径不正确，我是不会给你提供数据的！");

        }
    }

    @Override
    public int delete(@NonNull Uri uri, String selection, String[] selectionArgs) {
        int code = uriMatcher.match(uri);
        if (code == SUCCESS) {
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            int rowid = db.delete("info", selection, selectionArgs);
            if (rowid > 0) {
                Uri deleteUri = ContentUris.withAppendedId(uri, rowid);
                getContext().getContentResolver().notifyChange(deleteUri, null);
            }
            db.close();
            return rowid;
        } else {
            throw new IllegalArgumentException("路径不正确，我是不会给你提供数据的！");
        }
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues contentValues, @Nullable String selection, @Nullable String[] selectionArgs) {
        int code = uriMatcher.match(uri);
        if (code == SUCCESS) {
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            int rowid = db.update("info",contentValues,selection,selectionArgs);
            if (rowid > 0) {
                Uri updateUri = ContentUris.withAppendedId(uri, rowid);
                getContext().getContentResolver().notifyChange(updateUri, null);
            }
            db.close();
            return rowid;
        } else {
            throw new IllegalArgumentException("路径不正确，我是不会给你提供数据的！");
        }
    }
}
